package com.wpw.java8demo;

import org.junit.Test;

import java.util.*;

/**
 * @Description
 * @ClassName MapTest
 * @Author wupengwei
 * @Date 2022/3/29 17:14
 */
public class MapAndCollectionTest {

    @Test
    public void mapTest() {
        Map<String, String> map = new HashMap<>();
        map.put("a", "a");

        String b = map.getOrDefault("b", "b");
        System.out.println(b);

        map.put("b", null);
        String b2 = map.getOrDefault("b", "b");
        System.out.println(b2);

    }

    @Test
    public void computeIfAbsentTest() {
        Map<String, String> map = new HashMap<>();
        map.computeIfAbsent("a", key -> "a");
        map.computeIfAbsent("a", key -> "b");
        map.computeIfAbsent("c", key -> {
            System.out.println("这个key:" + key);
            return key;
        });
        System.out.println(map);
    }

    @Test
    public void putIfAbsentTest() {
        Map<String, String> map = new HashMap<>();
        map.computeIfAbsent("a", key -> "a");
        String s = map.putIfAbsent("a", "a2");
        System.out.println(s);//a
        String s1 = map.putIfAbsent("b", "b");
        System.out.println(s1);//null
        System.out.println(map);//{a=a, b=b}
    }

    @Test
    public void computeIfPresentTest() {
        Map<String, String> map = new HashMap<>();
        String a = map.computeIfPresent("a", (k, v) -> "a");
        System.out.println(a);
        System.out.println(map);
        map.put("a", "b");
        String a2 = map.computeIfPresent("a", (k, v) -> "a2");
        System.out.println(a2);
        System.out.println(map);
    }

    @Test
    public void removeIfTest() {
        List<String> list = new ArrayList<>();
        list.add("a");
        list.add("a");
        list.add("b");
        list.add("c");
        list.removeIf(v -> v.equals("a"));
        System.out.println(list);

    }
}
